
package net.swa.util;

import org.apache.commons.lang.math.RandomUtils;

/**
 * 字符串工具
 * @author 陈晓伟
 *
 */
public class StringUtil
{
    private static String numberAndLetter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /**
     * 随机生成指定个数的字符串，字符串从a-z A-Z
     * @param len 生成的字符串长度
     * @return 随机生成的字符串
     */
    public static String getRandomString(int len)
    {
        char[] chars = numberAndLetter.toCharArray();
        String str = "";
        for (int i = 0; i < len; i++)
        {
            char c = chars[RandomUtils.nextInt(numberAndLetter.length())];
            str += String.valueOf(c);
        }
        return str;
    }

    /**检查字符串中有特殊字符***/
    public static boolean checkStr(String str)
    {
        for (int i = 0; i < str.length(); i++)
        {
            int c = str.codePointAt(i);
            if (c < 0x0000 || c > 0xffff)
            {
                return false;
            }
        }
        return true;
    }

    /**检查字符串中有特殊字符***/
    public static String filterStr(String str)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++)
        {
            int c = str.codePointAt(i);
            if (c < 0x0000 || c > 0xffff)
            {
                //do nothing
                sb.append("*");
            }
            else
            {
                sb.append(Character.toChars(c));
            }
        }
        return sb.toString();
    }

}
